package com.qfedu.fmshop.service.Impl;

import cn.hutool.core.date.DateUtil;
import com.qfedu.fmshop.beans.User;
import com.qfedu.fmshop.common.utils.JwtUtils;
import com.qfedu.fmshop.common.utils.MD5Utils;
import com.qfedu.fmshop.common.vo.LoginVo;
import com.qfedu.fmshop.common.vo.ResStatus;
import com.qfedu.fmshop.common.vo.ResultVO;
import com.qfedu.fmshop.mapper.UsersMapper;
import com.qfedu.fmshop.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

@Slf4j
@Configuration
public class UserServiceImpl implements UserService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private UsersMapper usersMapper;


    /**
     * 检查用户登录
     * @param loginVo
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO CheckLogin(LoginVo loginVo) {
        log.info("用户登录UserService接收参数：{}",loginVo);
        log.info("用户登录UserService向UserMapper传入参数：{}",loginVo.getUsername());
        User result = usersMapper.selectUserByUsername(loginVo.getUsername());
        log.info("用户登录UserMapper返回参数：{}",result);
        if (result != null){

            if (MD5Utils.md5(loginVo.getPassword()).equals(result.getPassword())){
                //生成一个token
                String token = JwtUtils.createToken(result.getUsername(), UUID.randomUUID().toString());

                //账号密码正确
                return new ResultVO(ResStatus.OK,token,result);
            }else {
                //账号密码错误
                return new ResultVO(ResStatus.NO,ResStatus.LOGIN_MSG_FAIL,null);
            }

        }
        return new ResultVO(ResStatus.NO,ResStatus.UNKNOWN_ERROR,null);
    }



    /**
     * 用户注册
     * @param username
     * @param password
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO UserRegistration(String username, String password) {
        log.info("用户注册UserService接收参数：用户名：{}，密码：{}",username,password);
        //1.判断用户名是否存在
        User result = usersMapper.selectUserByUsername(username);
        if (result != null){
            //用户名已存在
            log.info("用户名已存在！！！");
            return new ResultVO(ResStatus.NO,ResStatus.REGIST_MSG_FAIL,null);
        }
        //用户名不存在  可以注册
        //2.完善用户数据
        User newUser = new User();
        newUser.setUsername(username);
        newUser.setPassword(MD5Utils.md5(password));
        newUser.setUserImg("img/tx.jpg");
        Date date = DateUtil.date();
        newUser.setUserRegtime(date);
        newUser.setUserModtime(date);
        log.info("新用户信息：{}",newUser) ;
        //3.存数据库
        int i = usersMapper.InsertNewUser(newUser);
        if (i>0){
            return new ResultVO(ResStatus.OK,ResStatus.REGIST_MSG_SUCCESS,null);
        }

        return new ResultVO(ResStatus.NO,ResStatus.UNKNOWN_ERROR,null);
    }
}
